package GUI; //done
import Common.Settings;

import java.awt.Font;
import java.awt.Rectangle;
import java.awt.SystemColor;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

import Client.ConnectionToServer;

import java.awt.Color;

import javax.swing.SwingConstants;
import javax.swing.DropMode;
import javax.swing.JTable;
import javax.swing.JCheckBox;

/**
 * This Class controls the log in menu.  
 * @author waleed
 * @author ameer
 * @author bshara	
 * @author kamil
 */
public class LoginMenu extends JFrame{
	
	/** The user name Label. */
	private JLabel usernameLabel = null;
	
	/** The password Label. */
	private JLabel  passwordLabel = null;
	
	/** The user name text Field. */
	private JTextField usernameField = null;
	
	/** The password Field. */
	private JPasswordField passwordField = null;
	
	/** The login Button. */
	private JButton loginButton = null;
	
	/** The content Pane. */
	private JPanel jContentPane = null;
	
	/** The Reseller Menu. */
	private ResellerMenu resellerMenu = null;
	
	/** The Customer Contact Employee Menu. */
	private CustomerContactEmployeeMenu customerContactEmployeeMenu = null;
	
	/** The Market Manager Menu. */
	private MarketManagerWindow marketmanagerFORM = null;	
	
	/** The Sales Man Menu. */
	private SalesManMenu salesManMenu = null;
	
	/** The Background picture Label. */
	private JLabel BackgroundLabel;
	
	/**
	 * The Default constructor
	 */
	public LoginMenu(){
		super();
		setResizable(false);
		initialize();		
	}
	
	/**
	 * This function initializes LoginMenu 
	 */
	private void initialize(){
		this.setBounds(Settings.Screen_x,Settings.Screen_y,603,507);
		this.setContentPane(getJContentPane());
		this.setTitle("CCRM Employee Login");
	}
	
	
	/**
	 * This function initializes usernameField	
	 */
	private JTextField getUsernameField(){
		if (usernameField == null) {
			usernameField = new JTextField();
			usernameField.setBounds(new Rectangle(287, 186, 130, 22));
		}
		return usernameField;
	}
	
	/**
	 * This function initializes passwordField	
	 */
	private JPasswordField getPasswordField(){
		if (passwordField == null) {
			passwordField = new JPasswordField();
			passwordField.setBounds(new Rectangle(287, 230, 130, 22));
		}
		return passwordField;
	}
	
	/**
	 * This function initializes loginButton	
	 */
	private JButton getloginButton(){
		if (loginButton == null) {
			loginButton = new JButton();
			loginButton.setIcon(new ImageIcon(LoginMenu.class.getResource("/Pictures/o.png")));
			loginButton.setBounds(new Rectangle(238, 291, 137, 29));
			loginButton.setText("   Log in");
			loginButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String message = "";
					try{
						String username = usernameField.getText();
						Settings.Logged_Username = usernameField.getText();
						char[] password = passwordField.getPassword();
						if (!username.equals("") && (password.length > 0)){
							ConnectionToServer connectiontoserver = new ConnectionToServer();
							message = (String)connectiontoserver.Login(username, password);
							while(true){	
								if (message != null){
									if (message.equals("AlreadyIN")) {
										JOptionPane.showMessageDialog(null, "You already LOGGED IN!!!","",JOptionPane.WARNING_MESSAGE);
										break;
									}	
									if (message.equals("Ilegalusername")){
										JOptionPane.showMessageDialog(null, "Elegal user name or password","",JOptionPane.WARNING_MESSAGE);
										break;
									}	
									if (message.equals("MarketDepardment")){
										resellerMenu = new ResellerMenu();
										setContentPane(resellerMenu);
										break;
									}
									if (message.equals("ClientContact")){	
										customerContactEmployeeMenu = new CustomerContactEmployeeMenu();
										setContentPane(customerContactEmployeeMenu);
										break;
									}
									if (message.equals("MarketManager")){
										marketmanagerFORM = new MarketManagerWindow();
										setContentPane(marketmanagerFORM);
										break;
									}		
									if (message.equals("SellsMen")){	
										setBounds(Settings.Screen_x,50,650,600);
										salesManMenu = new SalesManMenu(username);
										setContentPane(salesManMenu);
										break;
									}		
								}
								else
									JOptionPane.showMessageDialog(null, "ERROR!!!","CCRM ERROR",JOptionPane.ERROR_MESSAGE);
								break;
							}	
						}
						else
							JOptionPane.showMessageDialog(null, "ILEGAL INPUT", "WARNING!!!",JOptionPane.WARNING_MESSAGE);
						}
					catch (Exception errorexception){
						JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","CCRM ERROR",JOptionPane.ERROR_MESSAGE);						
					}							
				}
			});
		}
		return loginButton;
	}
	
	/**
	 * This function initializes jContentPane
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setBackground(SystemColor.activeCaption);
			jContentPane.setLayout(null);
			
			usernameLabel  = new JLabel();
			usernameLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
			usernameLabel.setBounds(new Rectangle(182, 185, 94, 22));
			usernameLabel.setText("User name:");
			
			passwordLabel = new JLabel();
			passwordLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
			passwordLabel.setBounds(new Rectangle(182, 229, 94, 22));
			passwordLabel.setText("Password:");
			
			jContentPane.add(usernameLabel, null);
			jContentPane.add(passwordLabel, null);
			jContentPane.add(getUsernameField(), null);
			jContentPane.add(getPasswordField(), null);
			jContentPane.add(getloginButton(),null);
			
			JLabel label = new JLabel("");
			label.setIcon(new ImageIcon(LoginMenu.class.getResource("/Pictures/login.png")));
			label.setBounds(249, 73, 143, 85);
			jContentPane.add(label);
												
			JLabel lblWelcomeToCcrm = new JLabel("Welcome to CCRM System");
			lblWelcomeToCcrm.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblWelcomeToCcrm.setForeground(Color.RED);
			lblWelcomeToCcrm.setBounds(212, 24, 205, 58);
			jContentPane.add(lblWelcomeToCcrm);
															
			BackgroundLabel = new JLabel("");
			BackgroundLabel.setIcon(new ImageIcon(LoginMenu.class.getResource("/Pictures/bg.png")));
			BackgroundLabel.setBounds(0, 0, 600, 480);
			jContentPane.add(BackgroundLabel);
			
		}
		return jContentPane;		
	}
}
